package com.tanhua.server.service;

import cn.hutool.core.collection.CollectionUtil;
import com.tanhua.dubbo.api.db.BlackListApi;
import com.tanhua.dubbo.api.db.QuestionApi;
import com.tanhua.dubbo.api.db.SettingsApi;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.model.db.*;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.model.vo.UserInfoVoAge;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SettingsService {

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    /**
     * 查询通用设置
     * @return
     */
    public SettingsVo getSettings() {
        //1. 获取登录用户信息
        User loginUser = UserHolder.getUser();
        //2. 通过用户id查询陌生人问题
        Question question = questionApi.findByUserId(loginUser.getId());
        //3. 通过用户id查询通知设置
        Settings settings = settingsApi.findByUserId(loginUser.getId());
        //4. 构建vo
        SettingsVo vo = new SettingsVo();
        vo.setStrangerQuestion(question==null?"你喜欢我吗?":question.getTxt());
        if(null != settings){
            BeanUtils.copyProperties(settings,vo);
        }
        vo.setId(loginUser.getId());
        vo.setPhone(loginUser.getMobile());
        //5. 返回
        return vo;
    }

    /**
     * 保存陌生人问题
     * @param paramMap
     */
    public void saveQuestion(Map<String, String> paramMap) {
        //1. 构建pojo
        Question question = new Question();
        //2. 调协pojo的值
        question.setTxt(paramMap.get("content"));
        question.setUserId(UserHolder.getUserId());
        //3. 调用api保存
        questionApi.save(question);
    }

    /**
     * 保存通知设置
     * @param vo
     */
    public void saveSettings(SettingsVo vo) {
        //1. vo转成pojo
        Settings pojo = new Settings();
        BeanUtils.copyProperties(vo, pojo);
        //2. 设置用户id
        pojo.setUserId(UserHolder.getUserId());
        //3. 调用api保存
        settingsApi.save(pojo);
    }

    /**
     * 黑名单分页查询
     * @param page
     * @param pageSize
     * @return
     */
    public PageResult<UserInfoVoAge> blackList(Long page, Long pageSize) {
        //1. 分页查询登录用户的黑名称列表数据, 只有黑名单人员的id
        // 【注意】参数的顺序
        PageResult pageResult = blackListApi.findPageByUserId(page,pageSize, UserHolder.getUserId());
        //2. 获取黑名单人员的详情
        List<BlackList> blackListList = pageResult.getItems();
        // 有分页结果集, 取出黑名单人员的id，查询用户详情，转成vo
        List<UserInfoVoAge> voList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(blackListList)) {
            //2.1 获取黑名单人员的id, stream流，把集合中的每个元素做处理，处理后再收集起来。使用什么来收集元素，使用List (Collectors.toList)
            //List<Long> blackUserIds = blackListList.stream().map(BlackList::getBlackUserId).collect(Collectors.toList());
            List<Long> blackUserIds = new ArrayList<>();
            for (BlackList blackList : blackListList) {
                // 取出每个黑名单人员的id
                Long blackUserId = blackList.getBlackUserId();
                blackUserIds.add(blackUserId);
            }
            //2.2 查询黑名单人员的详情
            List<UserInfo> userInfoList = userInfoApi.findByBatchIds(blackUserIds);
            //3. 转成vo
            for (UserInfo userInfo : userInfoList) {
                UserInfoVoAge vo = new UserInfoVoAge();
                BeanUtils.copyProperties(userInfo, vo);
                // 【注意】添加到voList
                voList.add(vo);
            }
        }
        // 重新设置分页结果为voList
        pageResult.setItems(voList);
        //4. 返回
        return pageResult;
    }

    /**
     * 黑名单 移除
     * @param blackUserId
     */
    public void removeBlackList(Long blackUserId) {
        //调用api移除
        blackListApi.remove(UserHolder.getUserId(), blackUserId);
    }
}
